package com.rentcars.controller;


import com.github.pagehelper.PageInfo;
import com.rentcars.entity.Resource;
import com.rentcars.entity.Role;
import com.rentcars.entity.RoleResource;
import com.rentcars.service.RoleResourceService;
import com.rentcars.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.sound.midi.Soundbank;
import javax.websocket.server.PathParam;
import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限管理-角色管理
 *
 * @author 肖克
 */
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleResourceService roleResourceService;

    @RequestMapping("")
    public ModelAndView toRoleList() {
        return new ModelAndView("role-list");
    }

    @RequestMapping("/toAddRole")
    public ModelAndView toAddRole() {
        return new ModelAndView("role-add");
    }

    @RequestMapping("/toRoleEdit/{id}")
    public ModelAndView toRoleEdit(@PathVariable Integer id) {
        Role role = roleService.selectById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role", role);
        modelAndView.setViewName("role-edit");
        return modelAndView;
    }

    /**
     * 分页查询
     *
     * @param pageNo   当前页数
     * @param pageSize 每页显示最大数
     * @return
     */
    @RequestMapping("/selectRoles")
    public PageInfo<Role> selectRoles(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(value = "pageSize") Integer pageSize) {
        PageInfo<Role> rolePageInfo = roleService.selectRoles(pageNo, pageSize);
        return rolePageInfo;
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     */
    @DeleteMapping("/deleteRole")
    public void deleteRole(@RequestParam(value = "roleid") Integer id) {
        //删除角色的权限信息
        roleResourceService.delete(id);
        //删除角色
        roleService.deleteRole(id);
    }

    /**
     * 批量删除角色
     *
     * @param id 角色id
     */
    @DeleteMapping("/deleteAllRoles")
    public void deleteAllRoles(Integer[] id) {
        //删除角色的权限信息
        for (Integer roleid : id) {
            //删除角色
            roleResourceService.delete(roleid);
        }
        roleService.deleteAllRoles(id);
    }

    /**
     * 添加角色
     *
     * @param role     角色对象
     * @param checkAll 权限
     */
    @RequestMapping("/addRole")
    public void addRole(@RequestBody Role role, @RequestParam(value = "checkAll", required = false) String[] checkAll) {
        int flag = roleService.addRole(role);
        //判断是否添加成功和是否设置权限
        if (flag == 1 && checkAll.length != 0) {
            for (String rid : checkAll) {
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleid(String.valueOf(role.getRoleid()));
                roleResource.setResourceid(rid);
                roleResourceService.addRoleSource(roleResource);
            }
        }
    }

    @RequestMapping("/edit")
    public void updateRoleAndResource(@RequestBody Role role, @RequestParam(value = "checkAll", required = false) String[] checkAll) {
        //更新角色数据
        roleService.updateRole(role);
        //修改角色权限
        roleResourceService.edit(role.getRoleid(), checkAll);


    }
}
